var jEvents = {
	addEvent: function(type, eventHandle, ele) {
					if(!ele) ele = this;
					if ( ele.addEventListener ) {
						ele.addEventListener( type, eventHandle, false );
					} else
					if ( ele.attachEvent ) {
						ele.attachEvent( "on" + type, eventHandle );
					} else {
						// assign each event handler a unique ID
						if (!eventHandle.$$guid) eventHandle.$$guid = jEvents.guid++;
						// create a hash table of event types for the element
						if (!ele.events) ele.events = {};
						// create a hash table of event handlers for each element/event pair
						var handlers = ele.events[type];
						if (!handlers) {
							handlers = ele.events[type] = {};
							// store the existing event handler (if there is one)
							if (ele["on" + type]) {
								handlers[0] = ele["on" + type];
							}
						}
						// store the event handler in the hash table
						handlers[eventHandle.$$guid] = eventHandle;
						// assign a global event handler to do all the work
						ele["on" + type] = jEvents.handleEvent;
					}
					return ele;
			},
	// a counter used to create unique IDs
	guid: 1,

	removeEvent: function(type, handler) {
					if (this.removeEventListener) {
						this.removeEventListener(type, handler, false);
					} else {
						// delete the event handler from the hash table
						if (this.events && this.events[type]) {
							delete this.events[type][handler.$$guid];
						}
					}
					return this;
				},

	handleEvent: function(event) {
						var returnValue = true;
						// grab the event object (IE uses a global event object)
						event = event || window.event;
						// create a hash table of event types for the element
						if (!this.events) this.events = {};
						// get a reference to the hash table of event handlers
						var handlers = this.events[event.type];
						// execute each event handler
						for (var i in handlers) {
							this.$$handleEvent = handlers[i];
							if (this.$$handleEvent(event) === false) {
								returnValue = false;
							}
						}
						return returnValue;
				},

	cancelEvents: function(event) {
					event = event || window.event;
					// add W3C standard event methods
					/*event.preventDefault = function() {
											  this.returnValue = false;
											};

					event.stopPropagation = function() {
											  this.cancelBubble = true;
											};*/

					if(event.preventDefault) event.preventDefault();
					if(event.stopPropagation) event.stopPropagation();

					return false;
				},
				
	trigger: function(asEvent) {
					if (document.createEvent) {
						e = document.createEvent("HTMLEvents");
						e.initEvent(asEvent, true, true);
						this.dispatchEvent(e);
					} else {
						e = document.createEventObject();
						e.eventType = asEvent;
						this.fireEvent(e.eventType, e);
					}

					return this;
				},
	on: function(asEvent, fn){
			if(!fn){
				if(typeof this[asEvent] == 'function')  this[asEvent]();
				this.trigger(asEvent);
			} else {
				this.addEvent(asEvent, fn);
			}
			return this;
		}
}

if (!window.addEventListener) {
	document.onreadystatechange = function() {
												if (window.onload && window.onload != jEvents.handleEvent) {
													jEvents.addEvent('load', window.onload, window);
													window.onload = jEvents.handleEvent;
												}
											}
}

/*("focus blur load resize scroll unload click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select keydown keypress keyup error")
.split(" ").each(function(value){
						jEvents[value] = function(fn){
											if(!fn){
												this.trigger(value, true);
											} else {
												this.addEvent(value, fn);
												return this;
											}
										}
					});*/

jarvis.extend(jEvents);